package com.sugree.jibjib;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

public class OAuthService {
	public static final String TAG = "OAuthService";

	private Context mContext;
	private SharedPreferences mSP;
	private OAuthConsumer mHttpOauthConsumer;
	private OAuthProvider mHttpOauthProvider;
	private String mRequestTokenUrl;
	private String mAccessTokenUrl;
	private String mAuthorizeUrl;

	public OAuthService(Context context) {
		mContext = context;
		PreferenceManager.setDefaultValues(mContext, R.xml.preferences, false);
		mSP = PreferenceManager.getDefaultSharedPreferences(mContext);

		mRequestTokenUrl = mSP.getString("oauth_request_token", "https://api.twitter.com/oauth/request_token");
		mAccessTokenUrl = mSP.getString("oauth_access_token", "https://api.twitter.com/oauth/access_token");
		mAuthorizeUrl = mSP.getString("oauth_authorize", "https://api.twitter.com/oauth/authorize");
		mHttpOauthConsumer = new CommonsHttpOAuthConsumer(PrivateData.OAUTH_CONSUMER_KEY, PrivateData.OAUTH_CONSUMER_SECRET);
		mHttpOauthProvider = new CommonsHttpOAuthProvider(mRequestTokenUrl, mAccessTokenUrl, mAuthorizeUrl);
	}

	public String doOAuth() {
		/*
		Log.d(TAG, "consumer key "+PrivateData.OAUTH_CONSUMER_KEY);
		Log.d(TAG, "consumer secret "+PrivateData.OAUTH_CONSUMER_SECRET);
		Log.d(TAG, "request token url "+mRequestTokenUrl);
		Log.d(TAG, "access token url "+mAccessTokenUrl);
		Log.d(TAG, "authorize url "+mAuthorizeUrl);
		*/

		try {
			String authUrl = mHttpOauthProvider.retrieveRequestToken(mHttpOauthConsumer, PrivateData.OAUTH_CALLBACK_URL);
			/*
			Log.d(TAG, "auth url "+authUrl);
			*/
			mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
		} catch (Exception e) {
			Log.d(TAG, "oauth exception "+e.getMessage());
			return e.getMessage();
		}
		return null;
	}

	public String callbackOAuth(Uri uri) {

		String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
		try {
			Log.d(TAG, "consumer "+mHttpOauthConsumer);
			Log.d(TAG, "verifier "+verifier);
			mHttpOauthProvider.retrieveAccessToken(mHttpOauthConsumer, verifier);
			Log.d(TAG, "provider "+mHttpOauthProvider);
			String accessToken = mHttpOauthConsumer.getToken();
			String tokenSecret = mHttpOauthConsumer.getTokenSecret();
			String username = mHttpOauthProvider
					.getResponseParameters()
					.getFirst("screen_name");
			/*
			Log.d(TAG, "access token "+accessToken);
			Log.d(TAG, "token secret "+tokenSecret);
			*/

			SharedPreferences.Editor esp = mSP.edit();
			if (username != null) {
				esp.putString("twitter_username", username);
			}

			esp.putString("oauth_access_token", accessToken);
			esp.putString("oauth_token_secret", tokenSecret);
			esp.putBoolean("use_oauth", true);
			esp.commit();
		} catch (Exception e) {
			Log.d(TAG, "oauth exception "+e.getMessage());
			return e.getMessage();
		}
		return null;
	}
}
